
(function(window) {

// 仅向外暴露这一个方法
// post为通过  wuchen/api/share.php 接口返回的数据。类似的还有 wuchen/hotshare.php

window.prepareShare = function(post, cb) {
    prepareMore(prepare(post), cb);
}

/*
    tid, fid, title, desc, sharegid, sharealias, out_site{host, path}, sharetype, wx_mini_id sharethumbnail, recommends, replies
*/
function prepare(post) {
    var shareid = makeShareId();
    var ts = timestamp();
	var q = {
		tid: post.tid,
		fid: post.fid,
		uid: app.auth.getMyId(),
		gid: post.sharegid,
		city: post.sharealias,
        shareid: shareid,
        sharetime: ts,
	};
	if (app.isLoggedIn()) {
		q.name = app.auth.getUserData().name;
	}

    var url = '';
    var qStr = toQueryStr(_queryExt(q, ''));
    if (obj_being(post.out_site)) {
        var out = post.out_site;
        var host = str_being(out.host) ? out.host : window.svrHosts.staticHost();
        url = host + out.path + '?' + qStr;
    } else {
        url = absAssetUrl(cfg.web_pages + 'web/views/post/index.html?' + qStr);
    }

	return {
		fid: post.fid,
		tid: post.tid,
		sharetype: post.sharetype,
		wx_mini_id: post.wx_mini_id,

		// 这组值用于提交服务端
		type: 1,
		objectid: post.tid,
        openfrom: post.openfrom,
        shareid: shareid,
        sharetime: ts,

		// 这组值用于分享出去展示
		title: post.subject,
        alias: post.sharealias,
		desc: filterTags(post.message).fix_substring(0, 100),
		thumbWeb: post.sharethumbnail,
		url: url,
        recommends: post.recommends,
        replies: post.replies,
        income: post.income,
        warning: post.warning,

        noTool: 1,
        showMini: 1,
	};
}

function prepareMore(post, cb) {
    var url1 = post.thumbWeb;
    var url2 = '';
    if (str_being(url1)) {
        // 如果有cover图，则让logo作为备选（以防cover图下载失败）
        url2 = httpGIdLogoUrl();
    } else {
        // 如果没有cover图，则直接以logo作为cover
        url1 = httpGIdLogoUrl();
        url2 = undefined;
    }

    downloadThumb(url1, url2, {
        succ: function(localUrl) {
            post.thumb = localUrl;

            if (cb && cb.succ) {
                cb.succ(post);
            } else {
                utils.rmLoading();
            	views.openShareFrame(post);
            }
        },
        fail: function() {
            if (cb && cb.fail) {
                cb.fail();
            } else {
                utils.rmLoading();
            }
        }
    });
}

// 两个url，优先使用url1，如果下载失败，则使用url2
function downloadThumb(url1, url2, cb) {
    api.imageCache({
        url: url1,
        thumbnail: false,
    }, function(ret, err) {
		if (ret.status) {
            cb && safeFn(cb.succ)(ret.url);
        } else {
            if (str_being(url2)) {
                api.imageCache({
                    url: url2,
                    thumbnail: false,
                }, function(ret, err) {
            		if (ret.status) {
                        cb && safeFn(cb.succ)(ret.url);
                    } else {
                        cb && safeFn(cb.fail)();
                    }
                });
            } else {
                cb && safeFn(cb.fail)();
            }
        }
    });
}

})(window);
